﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace VietImage.Models
{
    public partial class LogImage
    {
        #region Properties
        public User User
        {
            get
            {
                return this.GetUser();
            }
        }

        public Image Image
        {
            get
            {
                return this.GetImage();
            }
        }

        public Action Action
        {
            get
            {
                return this.GetAction();
            }
        }
        #endregion

        #region Methods
        public User GetUser()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Users.Where(p => p.ID == this.ID_User);
            if (v.Count() == 0)
                return null;
            return v.First();
        }

        public Image GetImage()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Images.Where(p => p.ID == this.ID_Image);
            if (v.Count() == 0)
                return null;
            return v.First();
        }

        public Action GetAction()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Actions.Where(p => p.ID == this.ID_Action);
            if (v.Count() == 0)
                return null;
            return v.First();
        }

        public List<ErrorItem> Rollback(int id_User, int id_Lang)
        {
            List<ErrorItem> errorList = new List<ErrorItem>();
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();

            String[] lContent = this.Content.Split('_');

            int errorMess = 0;
            try
            {

                if (lContent[0].Contains("[Detail]"))
                {
                    DetailImage di = new DetailImage();
                    di.ID_Image = this.ID_Image;
                    di.ID_Language = int.Parse(lContent[1]);
                    di.Name = lContent[2];
                    di.Description = lContent[3];
                    di.Tag = lContent[4];

                    errorList = DetailImage.ChangeInformation(di, id_User, this.ID, id_Lang);
                }
                else
                {
                    Image i = _db.Images.Single(p => p.ID == this.ID_Image);
                    i.Source = this.Source;
                    i.TakenTime = DateTime.Parse(lContent[0]);
                    i.ID_City = int.Parse(lContent[1]);
                    i.ID_User = int.Parse(lContent[2]);
                    i.ID_Status = int.Parse(lContent[3]);
                    i.Author = lContent[4];

                    if (this.Source == null)
                        errorList = Image.ChangeInformation(i, id_User, this.ID, id_Lang, 0);
                    else
                        errorList = Image.ChangeInformation(i, id_User, this.ID, id_Lang, 1);
                }
            }
            catch
            {
                errorMess = 48;
            }

            if (errorMess != 0)
                if(errorMess != 1) errorList.Add(new ErrorItem("ErrorMessage", _db.ErrorMessages.Single(p => p.ID_ErrorMessage == errorMess && p.ID_Language == id_Lang).Description));

            return errorList;
        }
        #endregion

        #region Static Methods
        public static List<LogImage> GetList()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            return _db.LogImages.OrderByDescending(p => p.ActionTime).ToList();
        }

        public static List<LogImage> GetList(int id_User)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            return _db.LogImages.Where(p => p.ID_User == id_User).OrderByDescending(p => p.ActionTime).ToList();
        }
        #endregion
        
    }
}